<?php
//Last Modified 2009-08-12

//file_put_contents(ROOT_DIR.'/data/pinyin.dat',serialize($pinyin_table));

$num_table = array(
	"0" => "ling",
	"1" => "yi",
	"2" => "er",
	"3" => "san",
	"4" => "si",
	"5" => "wu",
	"6" => "liu",
	"7" => "qi",
	"8" => "ba",
	"9" => "jiu",
	"0" => "ling",
	"a" => "a",
	"b" => "b",
	"c" => "c",
	"d" => "d",
	"e" => "e",
	"f" => "f",
	"g" => "g",
	"h" => "h",
	"i" => "i",
	"j" => "j",
	"k" => "k",
	"l" => "l",
	"m" => "m",
	"n" => "n",
	"o" => "o",
	"p" => "p",
	"q" => "q",
	"r" => "r",
	"s" => "s",
	"t" => "t",
	"u" => "u",
	"v" => "v",
	"w" => "w",
	"x" => "x",
	"y" => "y",
	"z" => "z",
);

$pinyin_table = false;

function getPinYin_Multi($string)
{
	global $pinyin_table;
	
	checkPinYinData();
	
	$flow = array();
	for ($i=0;$i<strlen($string);$i++)
	{
		if (ord($string[$i]) >= 0x81 and ord($string[$i]) <= 0xfe)
		{
			$h = ord($string[$i]);
			if (isset($string[$i+1])) 
			{
				$i++;
				$l = ord($string[$i]);
				if (isset($pinyin_table[$h][$l])) 
				{
					array_push($flow,$pinyin_table[$h][$l]);
				}
				else 
				{
					array_push($flow,$h);
					array_push($flow,$l);
				}
			}
			else 
			{
				array_push($flow,ord($string[$i]));
			}
		}
		else
		{
			array_push($flow,ord($string[$i]));
		}
	}

	//return $flow;
	
	$pinyin = array();
	$pinyin[0] = '';
	for ($i=0;$i<sizeof($flow);$i++)
	{
		if (is_array($flow[$i])) 
		{
			if (sizeof($flow[$i]) == 1)
			{
				foreach ($pinyin as $key => $value)
				{
					$pinyin[$key] .= $flow[$i][0];
				}
			}
			if (sizeof($flow[$i]) > 1)
			{
				$tmp1 = $pinyin;
				foreach ($pinyin as $key => $value)
				{
					$pinyin[$key] .= $flow[$i][0];
				}
				for ($j=1;$j<sizeof($flow[$i]);$j++)
				{
					$tmp2 = $tmp1;
					for ($k=0;$k<sizeof($tmp2);$k++)
					{
						$tmp2[$k] .= $flow[$i][$j];
					}
					array_splice($pinyin,sizeof($pinyin),0,$tmp2);
				}
			}
		}
		else 
		{
			foreach ($pinyin as $key => $value) 
			{
				$pinyin[$key] .= chr($flow[$i]);
			}
		}
	}
	return $pinyin;
}

function getPinYinAll($string)
{
	global $pinyin_table;
	global $num_table;
	
	checkPinYinData();
	
	$flow = array();
	for ($i=0;$i<strlen($string);$i++)
	{
		if (ord($string[$i]) >= 0x81 and ord($string[$i]) <= 0xfe) 
		{
			$h = ord($string[$i]);
			if (isset($string[$i+1])) 
			{
				$i++;
				$l = ord($string[$i]);
				if (isset($pinyin_table[$h][$l])) 
				{
					array_push($flow,$pinyin_table[$h][$l]);
				}
				else 
				{
					array_push($flow,$h);
					array_push($flow,$l);
				}
			}
			else 
			{
				array_push($flow,ord($string[$i]));
			}
		}
		else
		{
			array_push($flow,$string[$i]);
		}
	}

	$pinyin = "";
	foreach($flow as $v){
		$pinyin .= $v[0];
	}
	return $pinyin;
}

function getPinYin_Short($string)
{
	global $pinyin_table;
	
	checkPinYinData();
	
	$flow = array();
	for ($i=0;$i<strlen($string);$i++)
	{
		if (ord($string[$i]) >= 0x81 and ord($string[$i]) <= 0xfe) 
		{
			$h = ord($string[$i]);
			if (isset($string[$i+1])) 
			{
				$i++;
				$l = ord($string[$i]);
				if (isset($pinyin_table[$h][$l])) 
				{
					array_push($flow,$pinyin_table[$h][$l]);
				}
				else 
				{
					array_push($flow,$h);
					array_push($flow,$l);
				}
			}
			else 
			{
				array_push($flow,ord($string[$i]));
			}
		}
		else
		{
			array_push($flow,ord($string[$i]));
		}
	}

	$pinyin = "";
	foreach($flow as $v){
		$pinyin .= substr($v[0],0,1);
	}
	return $pinyin;
}

function checkPinYinData()
{
	global $pinyin_table;
	
	if(!$pinyin_table)
	{
		//载入拼音
		$pinyin_table	= unserialize(file_get_contents('./include/pinyin.dat'));
	}
}

?>